package com.schooleducation.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.StuDepartment;
import com.schooleducation.entity.StuMajor;
import com.schooleducation.entity.User;
import com.schooleducation.entity.UserInfo;
import com.schooleducation.entity.UserRoles;
import com.schooleducation.service.UserService;

public final class UserServiceImpl extends SuperService implements UserService {

	@Override
	public User doLogin(User u) {
		// TODO Auto-generated method stub
		if(u == null) {
			return null;
		}
		//数据合法验证
		if(u.getUserNum().length()<2 || u.getUserPassword().length() <6) {
			return null;
		}
		User user = super.getDaoFac().getUserDao().findByLoginParams(u);
		// 记录角色信息
		if (user != null) {
			user.setLastLoginDate(new Date());
			user.setLoginNum(user.getLoginNum() + 1);
		}

		return user;
	}

	@Override
	public String doRegist(User u) {
		// TODO Auto-generated method stub
		String userNum = null;
		// 验证数据合法性
		if ((u.getPetName().length() > 1 && u.getPetName().length() <= 12)
				&& (u.getUserPassword().length() >= 6 && u.getUserPassword().length() <= 16)) {
			// 验证昵称是否存在
			if (super.getDaoFac().getUserDao().findCountByPetName(u) == 0) {
				// 获取角色
				UserRoles ur = new UserRoles();
				ur.setRolesName("学生");
				u.setUserRoles(super.getDaoFac().getUserRolesDao().findByRolesName(ur));
				// 添加注册信息
				u.setCreateDate(new Date());
				u.setLogo("/SchoolEducationSystem/resource/image/user.jpg");
				u.setLoginNum(0);
				// 提交注册信息
				super.getDaoFac().getUserDao().save(u);
				// 添加附加信息
				UserInfo userInfo = new UserInfo();
				userInfo.setUser(u);
				super.getDaoFac().getUserInfoDao().save(userInfo);
				// 生成账号
				Calendar now = Calendar.getInstance();
				userNum = String.valueOf(now.get(Calendar.YEAR)) + (1000 + u.getId());
				u.setUserNum(userNum);
			}
		}

		return userNum;
	}

	@Override
	public boolean isRepeatPetName(User u) {
		// TODO Auto-generated method stub
		boolean flag = true;
		//检查昵称是否存在
		if (u != null && super.getDaoFac().getUserDao().findCountByPetName(u) == 0) {

			flag = false;
		}

		return flag;
	}

	@Override
	public List<LessonSubscribe> getUserListByLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		LessonSubscribe ls = new LessonSubscribe();
		ls.setLesson(lesson);
		//查看订阅课程的用户
		return super.getDaoFac().getLessonSubscribeDao().getSubscribeLessonByLesson(ls, 0, 25);
	}

	@Override
	public boolean auditStudent(int subscribeId, int state) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//数据合法性验证
		if (state == 0 || state == 2) {
			//审核用户
			LessonSubscribe ls = super.getDaoFac().getLessonSubscribeDao().findById(LessonSubscribe.class, subscribeId);
			
			if (ls !=null && ls.getState() != state) {
				ls.setState(state);
				flag = true;
			}
		}

		return flag;
	}

	@Override
	public UserInfo getUserInfo(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<StuDepartment> getStuDepartmentList() {
		// TODO Auto-generated method stub
		return super.getDaoFac().getStuDepartmentDao().findAll(StuDepartment.class);
	}

	@Override
	public boolean addStuDepartment(StuDepartment sd) {
		// TODO Auto-generated method stub
		super.getDaoFac().getStuDepartmentDao().save(sd);
		
		return true;
	}

	@Override
	public boolean updateStuDepartment(StuDepartment sd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		
		StuDepartment sdt = super.getDaoFac().getStuDepartmentDao().findById(StuDepartment.class, sd.getId());
		if(sdt != null) {
			sdt.setName(sd.getName());
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean deleteStuDepartment(int departmentId) {
		// TODO Auto-generated method stub
		StuDepartment sd = new StuDepartment();
		StuMajor sm = new StuMajor();
		sd.setId(departmentId);
		sm.setStuDepartment(sd);
		//删除相应专业
		super.getDaoFac().getStuMajorDao().deleteMajorByDepartment(sm);
		//删除系别
		super.getDaoFac().getStuDepartmentDao().deleteById(StuDepartment.class, departmentId);
		
		return true;
	}

	@Override
	public List<StuMajor> getStuMajorListByDepartment(int departmentId) {
		// TODO Auto-generated method stub
		StuDepartment sd = new StuDepartment();
		sd.setId(departmentId);
		StuMajor sm = new StuMajor();
		sm.setStuDepartment(sd);
		
		return super.getDaoFac().getStuMajorDao().getMajorListByDepartment(sm, 0, 0);
	}

	@Override
	public boolean addStuMajor(StuMajor sm) {
		// TODO Auto-generated method stub
		super.getDaoFac().getStuMajorDao().save(sm);
		
		return true;
	}

	@Override
	public boolean updateStuMajor(StuMajor sm) {
		// TODO Auto-generated method stub
		boolean flag = false;
		
		StuMajor sm2 = super.getDaoFac().getStuMajorDao().findById(StuMajor.class, sm.getId());
		if(sm2 != null) {
			sm2.setMajorName(sm.getMajorName());
			flag = true;
		}
		
		return flag;
	}

	@Override
	public boolean deleteStuMajor(int majorId) {
		// TODO Auto-generated method stub
		super.getDaoFac().getStuMajorDao().deleteById(StuMajor.class, majorId);
		
		return true;
	}

	@Override
	public List<User> getUserList(int start, int limit) {
		// TODO Auto-generated method stub
		List<User> users = super.getDaoFac().getUserDao().findByPage(User.class, start, limit);
		
		return users;
	}

	@Override
	public List<UserRoles> getUserRolesList() {
		// TODO Auto-generated method stub
		List<UserRoles> urs = super.getDaoFac().getUserRolesDao().findAll(UserRoles.class);
		
		return urs;
	}

	@Override
	public UserInfo getUserInfoAndMajorByUserId(int userId) {
		// TODO Auto-generated method stub
		UserInfo ui = new UserInfo();
		ui.setId(userId);
		ui = super.getDaoFac().getUserInfoDao().getInfoAndMajorById(ui);
		return ui;
	}

	@Override
	public boolean UpdateUserAndInfo(User u) {
		// TODO Auto-generated method stub
		super.getDaoFac().getUserDao().update(u);
		UserInfo ui = u.getUserInfo();
		ui.setId(u.getId());
		super.getDaoFac().getUserInfoDao().update(ui);
		
		return true;
	}

	@Override
	public Long getStudentCountByLesson(Lesson l) {
		// TODO Auto-generated method stub
		LessonSubscribe ls = new LessonSubscribe();
		ls.setLesson(l);
		
		Long num = super.getDaoFac().getLessonSubscribeDao().getLessonCount(ls);
		
		return num;
	}

	@Override
	public boolean updateUserRoles(User u) {
		// TODO Auto-generated method stub
		boolean flag = false;
		if(u.getId() != null) {
			User user = super.getDaoFac().getUserDao().findById(User.class, u.getId());
			user.setUserRoles(u.getUserRoles());
			flag = true;
		}
		
		return flag;
	}

	@Override
	public boolean updateUserLogo(User u) {
		// TODO Auto-generated method stub
		super.getDaoFac().getUserDao().update(u);
		
		return true;
	}

	@Override
	public boolean changePassword(User oldUser, String oldPassword, String newPassword) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//验证是否有用户、旧密码是否一致、密码长度
		if(oldUser!=null && oldUser.getUserPassword().equals(oldPassword)
				&& newPassword.length() >= 6) {
			oldUser.setUserPassword(newPassword);
			//更新密码
			super.getDaoFac().getUserDao().update(oldUser);
			flag = true;
		} 
		
		return flag;
	}

}
